1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using Excel = Microsoft.Office.Interop.Excel;
11 using System.IO;
12 namespace WarehouseManagementSystem
13 {
14 public partial class frmProductsRecord1 : Form
15 {
16 SqlDataReader rdr = null;
17 SqlConnection con = null;
18 SqlCommand cmd = null;
19 ConnectionString cs = new ConnectionString();
20 public frmProductsRecord1()
21 {
22 InitializeComponent();
23 }
24 public void GetData()
25 {
26 try
27 {
28 con = new SqlConnection(cs.DBConn);
29 con.Open();
30 cmd = new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID order by Productname", con);
31 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
32 dataGridView1.Rows.Clear();
33 while (rdr.Read() == true)
34 {
35 dataGridView1.Rows.Add(rdr[0],rdr[1],rdr[2],rdr[3],rdr[4],rdr[5],rdr[6],rdr[7],rdr[8]);
36 }
37 con.Close();
38 }
39 catch (Exception ex)
40 {
41 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
42 }
43 }
44 private void frmProductsRecord_Load(object sender, EventArgs e)
45 {
46 GetData();
47 }
48
49
50 private void Button3_Click(object sender, EventArgs e)
51 {
52 int rowsTotal = 0;
53 int colsTotal = 0;
54 int I = 0;
55 int j = 0;
56 int iC = 0;
57 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
58 Excel.Application xlApp = new Excel.Application();
59
60 try
61 {
62 Excel.Workbook excelBook = xlApp.Workbooks.Add();
63 Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[1];
64 xlApp.Visible = true;
65
66 rowsTotal = dataGridView1.RowCount;
67 colsTotal = dataGridView1.Columns.Count - 1;
68 var _with1 = excelWorksheet;
69 _with1.Cells.Select();
70 _with1.Cells.Delete();
71 for (iC = 0; iC <= colsTotal; iC++)
72 {
73 _with1.Cells[1, iC + 1].Value = dataGridView1.Columns[iC].HeaderText;
74 }
75 for (I = 0; I <= rowsTotal - 1; I++)
76 {
77 for (j = 0; j <= colsTotal; j++)
78 {
79 _with1.Cells[I + 2, j + 1].value = dataGridView1.Rows[I].Cells[j].Value;
80 }
81 }
82 _with1.Rows["1:1"].Font.FontStyle = "Bold";
83 _with1.Rows["1:1"].Font.Size = 12;
84
85 _with1.Cells.Columns.AutoFit();
86 _with1.Cells.Select();
87 _with1.Cells.EntireColumn.AutoFit();
88 _with1.Cells[1, 1].Select();
89 }
90 catch (Exception ex)
91 {
92 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
93 }
94 finally
95 {
96 //RELEASE ALLOACTED RESOURCES
97 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
98 xlApp = null;
99 }
100 }
101
102 private void frmProductsRecord_FormClosing(object sender, FormClosingEventArgs e)
103 {
104 this.Hide();
105 frmStock frm = new frmStock();
106 frm.lblUser.Text = lblUser.Text;
107 frm.lblUserType.Text = lblUserType.Text;
108 frm.Show();
109 }
110
111 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
112 {
113 string strRowNumber = (e.RowIndex + 1).ToString();
114 SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
115 if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
116 {
117 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
118 }
119 Brush b = SystemBrushes.ControlText;
120 e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
121
122 }
123
124 private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
125 {
126 try
127 {
128 DataGridViewRow dr = dataGridView1.SelectedRows[0];
129 this.Hide();
130 frmStock frm = new frmStock();
131 frm.Show();
132 frm.txtProductID.Text = dr.Cells[0].Value.ToString();
133 frm.txtProductName.Text = dr.Cells[1].Value.ToString();
134 frm.txtFeatures.Text = dr.Cells[6].Value.ToString();
135 frm.lblUser.Text = lblUser.Text;
136 frm.lblUserType.Text = lblUserType.Text;
137 }
138
139 catch (Exception ex)
140 {
141 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
142 }
143 }
144
145 private void txtProductname_TextChanged(object sender, EventArgs e)
146 {
147 try
148 {
149 con = new SqlConnection(cs.DBConn);
150 con.Open();
151 cmd = new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and ProductName like '" + txtProductname.Text + "%' order by Productname", con);
152 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
153 dataGridView1.Rows.Clear();
154 while (rdr.Read() == true)
155 {
156 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
157 }
158 con.Close();
159 }
160 catch (Exception ex)
161 {
162 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
163 }
164 }
165
166 private void txtCategory_TextChanged(object sender, EventArgs e)
167 {
168 try
169 {
170 con = new SqlConnection(cs.DBConn);
171 con.Open();
172 cmd = new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and CategoryName like '" + txtCategory.Text + "%' order by Productname", con);
173 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
174 dataGridView1.Rows.Clear();
175 while (rdr.Read() == true)
176 {
177 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
178 }
179 con.Close();
180 }
181 catch (Exception ex)
182 {
183 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
184 }
185 }
186
187 private void txtSubCategory_TextChanged(object sender, EventArgs e)
188 {
189 try
190 {
191 con = new SqlConnection(cs.DBConn);
192 con.Open();
193 cmd = new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and SubCategoryName like '" + txtSubCategory.Text + "%' order by Productname", con);
194 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
195 dataGridView1.Rows.Clear();
196 while (rdr.Read() == true)
197 {
198 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
199 }
200 con.Close();
201 }
202 catch (Exception ex)
203 {
204 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
205 }
206 }
207
208
209 }
210 }